package com.xiangban.yanglaodajie_platform.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.yanglaodajie_platform.dao.AccumulatePointsMapper;
import com.xiangban.yanglaodajie_platform.entity.AccumulatePoints;
import com.xiangban.yanglaodajie_platform.utils.CommonUtils;
import com.xiangban.yanglaodajie_platform.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xiangban.yanglaodajie_platform.utils.CommonUtils.getOrderNo;


/**
 * @Author Mr. MaYaYun
 * @Data 2021/3/30 10:06
 */
@Slf4j
@Service
public class AccumulatePointsService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private AccumulatePointsMapper accumulatePointsMapper;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private PayService payService;
    @Autowired
    private PageUtil pageUtil;

//    public int addAP(Map<String,Integer> map){
//        map.put("userId",Integer.parseInt(commonUtils.getUserId(request)));
//        AccumulatePoints accumulatePoints = new AccumulatePoints();
//        accumulatePoints.setUserId(map.get("userId"));
//        accumulatePoints.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//        accumulatePoints.setOrderNumber(new SimpleDateFormat("yy-MM-dd-HH-mm-ss").format(new Date()).replace("-","")+ RandomStringUtils.randomNumeric(8));
//        accumulatePoints.setType(1);
//        accumulatePoints.setAccumulatePoints(map.get("newPoints"));
//        int i = accumulatePointsMapper.updateAP(map);
//        accumulatePointsMapper.saveAP(accumulatePoints);
//        if (i==1){
//            return i;
//        }else {
//            return -1;
//        }
//    }

    public PageInfo<AccumulatePoints> getList(AccumulatePoints accumulatePoints) {
        String userId = commonUtils.getUserId(request);
        accumulatePoints.setUserId(Integer.parseInt(userId));

        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<AccumulatePoints> ordersByOrdersList = accumulatePointsMapper.getOrdersByOrdersList(accumulatePoints);
        return new PageInfo<>(ordersByOrdersList);
    }

    public Map<String, String> topUpIntegral(Map<String, Object> parameter) {
        String orderNo = getOrderNo();
        parameter.put("userId", commonUtils.getUserId(request));
        AccumulatePoints accumulatePoints = new AccumulatePoints();
        accumulatePoints.setUserId(Integer.parseInt(parameter.get("userId").toString()));
        accumulatePoints.setOrderNumber(orderNo);
        accumulatePoints.setType(1);
        accumulatePoints.setAccumulatePoints(new BigDecimal(parameter.get("newPoints").toString()));
        accumulatePoints.setPayState(1);
        accumulatePoints.setDescribe("积分充值");
        accumulatePoints.setRecodeType(0);
        accumulatePointsMapper.saveAP(accumulatePoints);
        HashMap<String, Object> unifiedParameter = new HashMap<>();
        unifiedParameter.put("order_no",orderNo);
        unifiedParameter.put("price",parameter.get("newPoints"));
        log.info("订单价格====="+parameter.get("newPoints"));
        return payService.doUnifiedOrder(unifiedParameter);

    }
    //告诉此订单编号支付成功了，修改积分记录表支付状态
    @Transactional
    public void updateTransaction(String orderNo){
        AccumulatePoints accumulatePoints = accumulatePointsMapper.getTransactionInfo(orderNo);
        if (accumulatePoints.getPayState() ==1 ){
            AccumulatePoints accumulatePoints1 = new AccumulatePoints();
            accumulatePoints1.setUserId(accumulatePoints.getUserId());
            accumulatePoints1.setAccumulatePoints(accumulatePoints.getAccumulatePoints());
            accumulatePointsMapper.updateAP(accumulatePoints1);
            accumulatePointsMapper.updateTransaction(orderNo);
        }


    }

}

